package com.lanou3g.baidumusic.main.musicfragment.listfragment;


import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.lanou3g.baidumusic.R;
import com.lanou3g.baidumusic.main.mainactivity.BaseFragment;
import com.lanou3g.baidumusic.main.tools.NetResultInterface;
import com.lanou3g.baidumusic.main.tools.NetWorkInstance;

/**
 * A simple {@link Fragment} subclass.
 */
public class ListFragment extends BaseFragment  {


    private ListFragmentAdapter adapter ;
    private RecyclerView recyclerView ;

    private String  urlStrs ="http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.9.9.3&channel=1413b&operator=1&method=baidu.ting.billboard.billCategory&format=json&kflag=2";


    public ListFragment() {
        // Required empty public constructor
    }


    @Override
    public int setLayout() {
        return R.layout.fragment_list;
    }

    @Override
    public void initView(View view) {

        recyclerView = (RecyclerView) view.findViewById(R.id.fragment_list_recycler_view_rv);

        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(staggeredGridLayoutManager);
//        recyclerView.addOnScrollListener(this);
    }


    @Override
    public void initData() {

        adapter = new ListFragmentAdapter(getContext());
        recyclerView.setAdapter(adapter);

        NetWorkInstance.getInstance().netRequest(urlStrs, new NetResultInterface() {
            //当网络请求成功了,自动调用此方法
            @Override
            public void sucess(String str) {
//                Log.d("success", str);

                //gson解析
                Gson gson = new Gson();
                //解析后的数据存于homeEntity中
                ListBean listBean = gson.fromJson(str , ListBean.class);
//                homeEntity.getData().getItems().get(1).getIntroduction();
                adapter.setListBean(listBean);

            }
            //网络请求失败了,自动调用次方法
            @Override
            public void faile(String str) {
                Log.d("faile" + "", str);
            }
        });


    }

}
